package com.byb.server.service.impl;

import com.byb.server.service.ExpertOpinionService;
import com.byb.server.domain.ExpertOpinion;
import com.byb.server.repository.ExpertOpinionRepository;
import com.byb.server.repository.search.ExpertOpinionSearchRepository;
import com.byb.server.service.dto.ExpertOpinionDTO;
import com.byb.server.service.mapper.ExpertOpinionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing ExpertOpinion.
 */
@Service
@Transactional
public class ExpertOpinionServiceImpl implements ExpertOpinionService{

    private final Logger log = LoggerFactory.getLogger(ExpertOpinionServiceImpl.class);

    private final ExpertOpinionRepository expertOpinionRepository;

    private final ExpertOpinionMapper expertOpinionMapper;

    private final ExpertOpinionSearchRepository expertOpinionSearchRepository;
    public ExpertOpinionServiceImpl(ExpertOpinionRepository expertOpinionRepository, ExpertOpinionMapper expertOpinionMapper, ExpertOpinionSearchRepository expertOpinionSearchRepository) {
        this.expertOpinionRepository = expertOpinionRepository;
        this.expertOpinionMapper = expertOpinionMapper;
        this.expertOpinionSearchRepository = expertOpinionSearchRepository;
    }

    /**
     * Save a expertOpinion.
     *
     * @param expertOpinionDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ExpertOpinionDTO save(ExpertOpinionDTO expertOpinionDTO) {
        log.debug("Request to save ExpertOpinion : {}", expertOpinionDTO);
        ExpertOpinion expertOpinion = expertOpinionMapper.toEntity(expertOpinionDTO);
        expertOpinion = expertOpinionRepository.save(expertOpinion);
        ExpertOpinionDTO result = expertOpinionMapper.toDto(expertOpinion);
        //expertOpinionSearchRepository.save(expertOpinion);
        return result;
    }

    /**
     *  Get all the expertOpinions.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ExpertOpinionDTO> findAll(Pageable pageable) {
        log.debug("Request to get all ExpertOpinions");
        return expertOpinionRepository.findAll(pageable)
            .map(expertOpinionMapper::toDto);
    }

    /**
     *  Get one expertOpinion by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public ExpertOpinionDTO findOne(Long id) {
        log.debug("Request to get ExpertOpinion : {}", id);
        ExpertOpinion expertOpinion = expertOpinionRepository.findOne(id);
        return expertOpinionMapper.toDto(expertOpinion);
    }

    /**
     *  Delete the  expertOpinion by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete ExpertOpinion : {}", id);
        expertOpinionRepository.delete(id);
        //expertOpinionSearchRepository.delete(id);
    }

    /**
     * Search for the expertOpinion corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ExpertOpinionDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of ExpertOpinions for query {}", query);
        Page<ExpertOpinion> result = expertOpinionSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(expertOpinionMapper::toDto);
    }

    /**
     *
     *  Get all the expertOpinions by ProductId.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<ExpertOpinionDTO> findAllByProductId(Long id, Pageable pageable) {
        return expertOpinionRepository.findAllByProductId(id,pageable).map(expertOpinionMapper::toDto);
    }
}
